<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
* FILE_NAME : resizeImage.class.php   FILE_PATH : \classes\libraries\resizeImage.class.php
* ....图片缩略图
*
* @copyright workflow_system 
* @author workflow Develop Team
* @package 
* @subpackage 
* @version Mon Oct 08 12:19:02 CST 2007
*/
class resizeImage{
	/**
	 * 图片类型
	 *
	 * @var string
	 */
	var $type;

	/**
	 * 实际图片宽度
	 *
	 * @var int
	 */
	var $width;

	/**
	 * 实际图片高度
	 *
	 * @var int
	 */
	var $height;

	/**
	 * 改变后的图片宽度
	 *
	 * @var int
	 */
	var $resize_width;

	/**
	 * 改变后的图片高度
	 *
	 * @var int
	 */
	var $resize_height;

	/**
	 * 是否截图
	 *
	 * @var int
	 */
	var $cut;

	/**
	 * 来源图片地址
	 *
	 * @var string
	 */
	var $srcimg;

	/**
	 * 目标图片地址
	 *
	 * @var string
	 */
	var $dstimg;

	/**
	 * 目标图片相对地址
	 *
	 * @var string
	 */
	var $relative_dstimg;

	/**
	 * 大图片地址
	 *
	 * @var string
	 */
	var $bigimg;

	/**
	 * 临时图片
	 *
	 * @var object
	 */
	var $im;

	/**
	 * 大图片高度
	 * 
	 */
	var $big_height;

	/**
	 * 大图片宽度
	 * 
	 */
	var $big_width;
	/**
	 * 缩略图文件名扩展
	 *
	 * 
	 */
	var $small_ext="_small.";
	/**
	 * 构造函数
	 *
	 * @param string $img
	 * @param int $wid
	 * @param int $hei
	 * @param int $c
	 */
	function resizeImage($img, $wid, $hei,$c,$small_ext="_small.",$dst_img='')
	{
		$this->srcimg = $img;
		$this->resize_width = $wid;
		$this->resize_height = $hei;
		$this->cut = $c;
		//缩略图后缀扩展
		$this->small_ext = $small_ext;
		//缩略图存放位置
		$this->dstimg = $dst_img;
		//图片的类型
		$this->type = substr(strrchr($this->srcimg,"."),1);
		//初始化图象
		$this->initi_img();
		
		//目标图象地址
		$this->dst_img();

		//--
		$this->width = imagesx($this->im);
		$this->height = imagesy($this->im);
		//生成图象
		$this->newimg();
		ImageDestroy ($this->im);
	}
    /*
	function __construct($img, $wid, $hei,$c,$small_ext="_small."){
		$this->resizeImage($img, $wid, $hei,$c,$small_ext="_small.");
	}
	*/
	/**
	 * 生成新的图片
	 *
	 */
	function newimg()
	{
		//改变后的图象的比例
		$resize_ratio = ($this->resize_width)/($this->resize_height);
		//实际图象的比例
		$ratio = ($this->width)/($this->height);
		
		if(($this->cut)=="1")
		//裁图
		{
			if($ratio>=$resize_ratio)
			//高度优先
			{
				$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
				imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
				ImageJpeg ($newimg,$this->dstimg);
			}
			if($ratio<$resize_ratio)
			//宽度优先
			{
				$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
				imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
				ImageJpeg ($newimg,$this->dstimg);
			}
		}
		else
		//不裁图
		{
			$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
			imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, $this->height);
			ImageJpeg ($newimg,$this->dstimg);
		}
	}

	/**
	 * 初始化图象
	 *
	 */
	function initi_img()
	{
		if($this->type=="jpg")
		{
			$this->im = @imagecreatefromjpeg($this->srcimg);
			if ($this->im == ""){
				$report = "This picture is not a valid jpg file!";
			}
		}
		if($this->type=="gif")
		{
			$this->im = @imagecreatefromgif($this->srcimg);
			if ($this->im == ""){
				$report = "This picture is not a valid gif file!";
			}
		}
		if($this->type=="png")
		{
			$this->im = imagecreatefrompng($this->srcimg);
			if ($this->im == ""){
				$report = "This picture is not a valid png file!";
			}
		}
		/*如果出错则报错*/
		if ($this->im == ""){
			/*删除该图片*/
			//@unlink($this->srcimg);
			Common::outMessage("json",$report,0);
		}
	}

	/**
	 * 图象目标地址
	 *
	 */
	function dst_img()
	{
		if($this->dstimg == ''){
			$full_length = strlen($this->srcimg);
			$type_length = strlen($this->type);
			$name_length = $full_length-$type_length;
			$name = substr($this->srcimg,0,$name_length-1);
			$this->dstimg = $name.$this->small_ext.$this->type;
		}else{
			$line = str_replace('\\','/',$this->srcimg);//整理文件名间隔符
			$img = explode('/',$line);
			$file = explode('.',$img[count($img)-1]);
			$this->dstimg = $this->dstimg.'/'.$file[0].$this->small_ext.$file[1];
		}
		//目标文件相对路径
		$this->relative_dstimg = str_replace(BasePath,'',$this->dstimg);
	}
}
?>